require('../../mypublic/css/common.less');
require('../../mypublic/css/index.less');

require('../../public/misc/mediaUpload/css/style.css');
require('../../public/misc/jquery.jqGrid-4.6.0/css/ui.jqgrid.css');
require('../../public/misc/jquery-ui-themes-1.11.2/humanity/jquery-ui.min.css');


require("jquery");

require('../../public/misc/jquery.jqGrid-4.6.0/js/i18n/grid.locale-cn');
require('../../public/misc/jquery.jqGrid-4.6.0/js/jquery.jqGrid.min');

require('../../public/js/myservice/side');
require("../../public/js/myservice/authorize");

var angular = require('angular');

angular.module('app', [])
    .controller('mainCtrl', ['$scope', '$http', function ($scope, $http) {
        $scope.data = {};

        $http.get('/api/index.php?r=/cms/media/get-dirs')
            .success(function (res) {
                $scope.data.dirs = res.data;

                oriFunc();
            })
    }]);

angular.bootstrap(document, ['app']);

function oriFunc() {
    $(document).ready(function () {
        $('#search-btn').bind('click', function () {
            var keyword = $('#search-keyword').val();
            var dir_id = $('#dir-selector').val();
            var status = $('#status-selector').val();
            $(this).attr('disabled', 'disabled');
            $('#grid-table').setGridParam(
                {
                    postData: {
                        keyword: keyword,
                        dir_id: dir_id,
                        status: status
                    }
                }
            );
            $('#grid-table').trigger('reloadGrid');
            $('#clean-keyword-btn').css({display: ''});
            $('#search-keyword').focus();
        });

        $('#search-keyword').bind('keypress', function (e) {
            if (e.keyCode == 13) {
                $('#search-btn').trigger('click');
                return false;
            } else if (e.keyCode == 27) {
                $('#clean-keyword-btn').trigger('click');
                $(this).val('').blur().focus();
                return false;
            }
        }).focus();

        $('#clean-keyword-btn').bind('click', function () {
            $('#search-keyword').val('').focus();
            $('#dir-selector option:first').prop('selected', true);
            $('#status-selector option:nth-child(2)').prop('selected', true);
            $('#grid-table').setGridParam({
                postData: {
                    keyword: $('#search-keyword').val(),
                    dir_id: $('#dir-selector').val(),
                    status: $('#status-selector').val()
                }
            });
            $('#grid-table').trigger('reloadGrid');
            $(this).hide();
        });

        $('#grid-table').jqGrid(
            {
                url: '/api/index.php?r=/official/media/index',
                colNames: [
                    '目录',
                    '说明',
                    '资源地址',
                    '大小',
                    '尺寸',
                    '类型',
                    '状态',
                    '创建时间',
                    '修改时间'
                ],
                colModel: [
                    {index: 'dir_id', name: 'dir_id', align: 'center', width: 50},
                    {index: 'name', name: 'name', align: 'left', width: 60, editable: true},
                    {index: 'file', name: 'file', align: 'center', width: 120},
                    {index: 'size', name: 'size', align: 'center', width: 40},
                    {index: 'image_info', name: 'image_info', align: 'center', width: 40},
                    {index: 'type', name: 'type', align: 'center', width: 40},
                    {
                        index: 'status',
                        name: 'status',
                        width: 40,
                        align: 'center',
                        editable: true,
                        edittype: 'checkbox',
                        formatter: 'checkbox',
                        editoptions: {value: '1:0'}
                    },
                    {index: 'created_time', name: 'created_time', align: 'left', width: 60},
                    {index: 'modified_time', name: 'modified_time', align: 'left', width: 60}
                ],
                datatype: 'json',
                height: 'auto',
                autowidth: true,
                rowNum: 20,
                rowList: [10, 20, 50, 100],
                viewrecords: true,
                viewsortcols: 'vertical',
                hoverrows: true,

                pager: $('#grid-pager'),
                sortname: 'id',
                sortorder: 'asc',
                cellEdit: true,
                cellurl: '/api/index.php?r=/official/media/quick-set',
                afterSubmitCell: function (serverresponse, rowid, cellname, value, iRow, iCol) {
                    if ('' != serverresponse.responseText) {
                        var response = $.parseJSON(serverresponse.responseText);
                        return [false, response.errMsg];
                    } else return [true];
                },
                loadComplete: function () {
                    loadingAuth();
                    $('#search-btn').attr('disabled', false);
                },
                ondblClickRow: function (id) {
                    window.location.href = '/official/media_edit/index.html?id=' + id;
                }
            }
        ).navGrid(
            '#grid-pager',
            // options
            {
                search: false,
                addfunc: function () {
                    window.location.href = '/official/media_edit/index.html';
                },
                editfunc: function (id) {
                    window.location.href = '/official/media_edit/index.html?id=' + id;
                },
                del: false
            },
            // edit
            {},
            // add
            {},
            // del
            {
                url: '/api/index.php?r=/official/media/delete'
            }
        );
    });
}
function loadingAuth(){
}